// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import store from './vuex/index'
import api from './api/index'

Vue.prototype.$api = api
Vue.config.productionTip = false
Vue.use(Vuex)
router.beforeEach((to, from, next) => {
  if (store.state.user === null) {
    api.getUserInfo().then(function (data) {
      if (data.code === 0) {
        store.commit('updateUser', data.data.user)
      } else {
        if (to.matched.some(record => record.meta.auth)) {
          next({
            path: '/login',
            query: to.fullPath
          })
        }
      }
    }).then(function () {
      next()
    })
  } else {
    next()
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
